<?php
/**
 * 管理企业登录用户
 * jzzmly
 */
class EAdmin_Page_UserManage extends EAdmin_Page_Abstract
{
	private $resumeDb;
	private function loadDb()
	{
		$this->resumeDb = Db_Resume::instance();
	}
	public function validate(ME_Request $input, ME_Response $output)
	{
		$euserInfo = Libs_EUser::getLoginStatus($input,$output);
		$sectionArr = Libs_EUser::getUserSection($euserInfo);
		print_r($sectionArr);
		$output->euid =
		$input->euid =
		$euid = $euserInfo[0];

		if(empty($euid)){
			header("location:index.php?c=login");
		}

		$state = ME_Config::get("ResumeState");
		$output->state = $state['state'];
		$output->eduDegree = $eduDegree = $state['eduDegree'];

		$dataArr = Libs_Admin_Category::getCategoryList(array('module_type'=>1,'subclassid'=>0,'type'=>1,'pagesize'=>1000));
		$newDataArr = array();
		if($dataArr){
			foreach($dataArr as $data){
				$id = !empty($data['id']) ? $data['id'] : 0;
				if($id == 0){
					continue;
				}
				$newDataArr[$id] = $data['name'];
			}
		}
		$output->cityArr = $newDataArr;


		$stateinfo = Libs_EState::getEResumeState($euid);
		if(!empty($stateinfo)){
			$state_arr = explode(",",$stateinfo);
		}else{
			$state_arr = array();
		}

		$output->state_arr = $state_arr;


		return true;
	}

	public function doDefault(ME_Request $input, ME_Response $output)
	{
		die('错误的请求');
	}


	//get list
	public function doList(ME_Request $input, ME_Response $output)
	{
		$condition = " and firm_id='{$output->euid}'";
		$db = Db_Resume::instance();

		$pageSize = 20;
		$page = $input->get("page");
		if(empty($page)){
			$page = 1;
		}
		$from = ($page-1)*$pageSize;

		//delete
		if($input->post('delBtn')){
			$idArr = $input->post('id');
			if($idArr){
				$more .= ' and id in('.implode(',',$idArr).')';
				$db->query("Update `e_sub_user` Set status=1 Where 1 {$condition} {$more}");
			}
		}

		//query
		$pageUrl = $input->baseUrl.'/index.php?c=UserManage&a=List';
		$name = $input->request('name');
		if($name){
			$output->name = $name;
			$condition .= 'and name like "%'.$name.'%" ';
			$pageUrl .= "&name={$name}";
		}
		
		//pageing
		$totalNum = $db->getOne("Select count(*) From `e_sub_user` Where status=0 {$condition}");
		$output->pageStr = Libs_GlobalFunc::multiPage(array(
		'page' => $page,
		'pageSize' => $pageSize,
		'total' => $totalNum,
		'baseUrl' => $pageUrl."&page="
		));
		
		$sql = "Select * From `e_sub_user` Where status=0 {$condition} limit {$from},{$pageSize}";
		$output->dataList =
		$dataList = $db->getAll($sql);
		
		//section arr
		$output->sectionArr = Libs_EDbHand::getFirmSectionArr(array('firmId'=>$output->euid));

		$output->setTemplate("UserList");
	}

	//add
	public function doAdd(ME_Request $input, ME_Response $output)
	{
		$condition = " and firm_id='{$output->euid}'";
		//post
		if($input->post('addUser')){
			$addParam = '';
			$addParam['firm_id'] = $output->euid;
			$addParam['firm_tag'] = $output->euid; //记录企业的别名，子用户登录时要提供，方便区别同名
			$addParam['name'] = trim($input->post('name'));
			$addParam['passwd'] = md5(trim($input->post('passwd'))); //md5加密
			$addParam['section_id'] = trim($input->post('section_id'));
			$addParam['real_name'] = trim($input->post('real_name'));
			$addParam['mobile'] = trim($input->post('mobile'));
			$addParam['e_desc'] = trim($input->post('digest'));
			$addParam['add_time'] = date('Y-m-d H:i:s');

			//check has exists
			$db = Db_Resume::instance();
			$isExists = $db->getOne("Select 'X' From `e_sub_user` Where 1 {$condition} and name='{$addParam['name']}'");
			if($isExists) self::showMsg('该用户已经存在','/index.php?c=UserManage&a=Add');
			//add
			$insertId = Libs_EDbHand::addSubUser(array('arr'=>$addParam));
			if($insertId){
				self::showMsg('添加成功','/index.php?c=UserManage&a=List');
			} else {
				self::showMsg('网络连接失败，请稍候再试','index.php?c=UserManage&a=Add');
			}
		}
		//section arr
		$sectionArr = Libs_EDbHand::getFirmSectionArr(array('firmId'=>$output->euid));
		$output->eSectionStr = Libs_GlobalFunc::createSelectElement($sectionArr,'section_id','section_id','管理部门','');

		$output->setTemplate("UserAdd");
	}
	
	//edit
	public function doEdit(ME_Request $input, ME_Response $output)
	{
		$id = $input->request('id'); //编辑的id
		if(!$id) die('您要请求的内容不存在');

		$condition = " and firm_id='{$output->euid}' and id='{$id}'";
		$db = Db_Resume::instance();
		$sql = "Select * From `e_sub_user` Where 1 {$condition} ";
		$dataInfo = $db->getRow($sql);
		if(!$dataInfo) die('您要请求的内容不存在');

		//post
		if($input->post('updateSubmit')){
			$addParam = '';
			$pwd = trim($input->post('passwd'));
			
			$addParam['name'] = trim($input->post('name'));
			$pwd && $addParam['passwd'] = md5($pwd);
			$addParam['real_name'] = trim($input->post('real_name'));
			$addParam['section_id'] = trim($input->post('section_id'));
			$addParam['mobile'] = intval($input->post('mobile'));
			$addParam['e_desc'] = trim($input->post('digest'));

			//update
			$res = Libs_EDbHand::updateSubUser(array('arr'=>$addParam,'where'=>" where id='{$id}' and firm_id='{$output->euid}'"));
			if($res){
				self::showMsg('更新成功','/index.php?c=UserManage&a=List');
			} else {
				self::showMsg('网络连接失败，请稍候再试','index.php?c=UserManage&a=Edit&id='.$id);
			}
			die();
			
		}
		//section arr
		$sectionArr = Libs_EDbHand::getFirmSectionArr(array('firmId'=>$output->euid));
		$output->eSectionStr = Libs_GlobalFunc::createSelectElement($sectionArr,'section_id','section_id','管理部门',$dataInfo['section_id']);

		//赋值
		$output->dataInfo = $dataInfo;

		$output->setTemplate("UserEdit");
	}


}
